package content;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import javax.xml.ws.Response;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class DelXml {
	private String url = "c:/PageXmlDefault.xml";
	private String str = "<" + "?xml" + " " + "version=\"1.0\" " + "encoding="
			+ "\"UTF-8\"?>" + "\n" + "<pageview_list>" + "\n";
	private Element RootList;
	private List list;
	private String delfilename;
	private int num;
	private int num2;
	private int dnum;
	private FileWriter writer;
	private String index;

	public DelXml(String index, String delfilename) {
		this.index = index;
		this.delfilename = delfilename;
		del();
	}

	public List returnXMLList(String xmlUrl) throws Exception {
		SAXBuilder oBuilder = new SAXBuilder();
		Document oDoc = oBuilder.build(new File(xmlUrl));

		Element xmlRoot = oDoc.getRootElement(); // root element
		List RootList = xmlRoot.getChildren();

		return RootList;
	}

	public void del() {

		try {
			String filePath = "c:/PageXmlDefault.xml";
			File file = new File(filePath);
			list = this.returnXMLList(url);
			for (int i = 0; i < list.size(); i++) {
				
				RootList = (Element) list.get(i);
				num = Integer.parseInt(RootList.getAttributeValue("linkpageindex"));
				dnum = Integer.parseInt(index);
				num2 = i+1;

				if (dnum == num)
					continue;
				

				str += "<" + RootList.getName() + " " + "index= \""
						+ RootList.getAttributeValue("index") + "\"" + " "
						+ "linkpageindex=\""
						+ num2 + "\""
						+ " " + "pagename=\""
						+ RootList.getAttributeValue("pagename") + "\"" + " />"
						+ "\n";

			}

			str += "</pageview_list>";
			File del = new File("c:/" + delfilename + ".xml");
			del.delete();
			outXml(filePath);

		} catch (Exception e) {

		}
	}

	public void outXml(String filePath) {
		try {
			writer = new FileWriter(filePath);
			writer.write(str);
		} catch (IOException e) {
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
			}
		}
	}
}
